package cc.eddic.pratice.programmingparadigms.emp02list.impl;

import cc.eddic.pratice.programmingparadigms.Result;
import cc.eddic.pratice.programmingparadigms.ResultComparator;
import cc.eddic.pratice.programmingparadigms.emp02list.ResultList;

import java.util.Objects;

public class ResultListLhImpl implements ResultList {

    private Result[] results;


    @Override
    public void add(Result result) {
        if (null == result)
            throw new IllegalArgumentException("result is null");
        if (size() == results.length) {
            Result[] newresult = new Result[results.length * 2];
            if (size() >= 0) System.arraycopy(results, 0, newresult, 0, size());
            results = newresult;
        }
        results[size()] = result;
    }

    @Override
    public int size() {
        int number = 0;
        for (Result result : results) {
            if (result != null) number++;
            else break;
        }
        return number;
    }

    /**
     * @return results数组被分配了多少空间
     */
    @Override
    public int capacity() {
        return results.length;
    }

    @Override
    public Result get(int index) {
        if (index >= results.length || size() == 0 || index >= size()) {
            String s = "Index: " + index + ", Size: " + size();
            throw new IndexOutOfBoundsException(s);
        } else
            return results[index];

    }

    @Override
    public Result findByGender(String gender) {
        for (int i = 0; i < size(); i++) {
            if (Objects.equals(results[i].getKey(), gender))
                return results[i];
        }
        return null;
    }

    @Override
    public Result findByDepartment(String department) {
        for (int i = 0; i < size(); i++) {
            if (Objects.equals(results[i].getKey(), department))
                return results[i];
        }
        return null;
    }

    @Override
    public Result findByJobTitle(String jobTitle) {
        for (int i = 0; i < size(); i++) {
            if (Objects.equals(results[i].getKey(), jobTitle))
                return results[i];
        }
        return null;
    }

    public ResultListLhImpl(int capacity) {
        results = new Result[capacity];
        int index = 0;
    }

    public ResultListLhImpl() {
        this(10);
    }

    public ResultListLhImpl(Result[] results) {
        this(results.length * 2);
        for (Result result : results) {
            this.add(result);
        }
    }

    public ResultListLhImpl(ResultList result) {
        this(result.size() * 2);
        for (int i = 0; i < result.size(); i++) {
            this.add(result.get(i));
        }
    }
    public void sort(ResultComparator comparator) {
        for (int i = 0; i < size(); i++) {
            for (int j = 0; j < size() - i - 1; j++) {
                if (comparator.compare(results[j], results[j + 1]) > 0) {
                    Result temp = results[j];
                    results[j] = results[j + 1];
                    results[j + 1] = temp;
                }
            }
        }
    }
}
